package com.ccx.coachsite.versionupdate;


import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.accessibility.AccessibilityEvent;

import com.ccx.coachsite.R;


public class WaitingView {
	public static View showView = null;
	public static WindowManager mWindowManager = null;
	public static Window mWindow = null;
	public static ProgressDialog progressDialog;
	
	public static void showProgress(Context ctx, String message) {
		if (null != message) {
			progressDialog = ProgressDialog.show(ctx, ctx.getString(R.string.login_waiting_title), 
					message, true, true);
		}else{
			progressDialog = ProgressDialog.show(ctx, ctx.getString(R.string.login_waiting_title), 
					ctx.getString(R.string.login_waiting_message), true, true);
		}
	}

	public static void showProgress(Context ctx, long time) {
		showProgress(ctx, null);
		Message msg = mHandler.obtainMessage(0, showView);
		mHandler.sendMessageDelayed(msg, time);
	}

	public static Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			View view = (View) msg.obj;
			if (view == showView) {
				cancelProgress();
			}
		}
	};

	public static void cancelProgress() {
		try {
			progressDialog.dismiss();
		} catch (Exception e) {
		}
	}

	public static class WaitingViewCallBack implements Window.Callback {

		@Override
		public boolean dispatchKeyEvent(KeyEvent event) {
			if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
				cancelProgress();
				return true;
			}
			return false;
		}

		@Override
		public boolean dispatchTouchEvent(MotionEvent event) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean dispatchTrackballEvent(MotionEvent event) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void onContentChanged() {
			// TODO Auto-generated method stub

		}

		@Override
		public boolean onCreatePanelMenu(int featureId, Menu menu) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public View onCreatePanelView(int featureId) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean onMenuItemSelected(int featureId, MenuItem item) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onMenuOpened(int featureId, Menu menu) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void onPanelClosed(int featureId, Menu menu) {
			// TODO Auto-generated method stub

		}

		@Override
		public boolean onPreparePanel(int featureId, View view, Menu menu) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onSearchRequested() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void onWindowAttributesChanged(LayoutParams attrs) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onWindowFocusChanged(boolean hasFocus) {
			// TODO Auto-generated method stub

		}
		
		public void onAttachedToWindow(){
			
		}
		
		public void onDetachedFromWindow(){}

		@Override
		public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent arg0) {
			// TODO Auto-generated method stub
			return false;
		}

	}
}
